<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将字符串转换为数组</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.str-split" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">str_split</h1>
  <p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">str_split</span> &mdash; <span class="dc-title">将字符串转换为数组</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.str-split-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>str_split</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$split_length</code><span class="initializer"> = 1</span></span>
  ] ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   将一个字符串转换为数组。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.str-split-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       输入字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">split_length</code></dt>

     <dd>

      <p class="para">
       每一段的长度。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.str-split-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果指定了可选的 <code class="parameter">split_length</code> 参数，返回数组中的每个元素均为一个长度为 <code class="parameter">split_length</code> 的字符块，否则每个字符块为单个字符。
  </p>
  <p class="para">
   如果 <code class="parameter">split_length</code> 小于 1，返回 <strong><code>FALSE</code></strong>。如果 <code class="parameter">split_length</code> 参数超过了 <code class="parameter">string</code> 超过了字符串 <code class="parameter">string</code> 的长度，整个字符串将作为数组仅有的一个元素返回。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.str-split-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5960">
    <p><strong>Example #1 <span class="function"><strong>str_split()</strong></span> 使用范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;Friend"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$arr1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_split</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$arr2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_split</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; H
    [1] =&gt; e
    [2] =&gt; l
    [3] =&gt; l
    [4] =&gt; o
    [5] =&gt;
    [6] =&gt; F
    [7] =&gt; r
    [8] =&gt; i
    [9] =&gt; e
    [10] =&gt; n
    [11] =&gt; d
)

Array
(
    [0] =&gt; Hel
    [1] =&gt; lo
    [2] =&gt; Fri
    [3] =&gt; end
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.str-split-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    在处理多字节字符时，<span class="function"><strong>str_split()</strong></span> 会按字节数转换，而非字符数。
   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.str-split-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="chunk_split.html" class="function" rel="rdfs-seeAlso">chunk_split()</a> - 将字符串分割成小块</span></li>
    <li class="member"><span class="function"><a href="preg_split.html" class="function" rel="rdfs-seeAlso">preg_split()</a> - 通过一个正则表达式分隔字符串</span></li>
    <li class="member"><span class="function"><a href="explode.html" class="function" rel="rdfs-seeAlso">explode()</a> - 使用一个字符串分割另一个字符串</span></li>
    <li class="member"><span class="function"><a href="count_chars.html" class="function" rel="rdfs-seeAlso">count_chars()</a> - 返回字符串所用字符的信息</span></li>
    <li class="member"><span class="function"><a href="str_word_count.html" class="function" rel="rdfs-seeAlso">str_word_count()</a> - 返回字符串中单词的使用情况</span></li>
    <li class="member"><a href="control_structures.for.html" class="link">for</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>